-- UITankDesc
-- Create by cheny3
-- 罐子说明界面

-- UITankDesc继承自Layer
UITankDesc = class("UITankDesc", function()
    return cc.Layer:create();
end);

function UITankDesc.create(tankType, campId, extra)
    return UITankDesc.new(tankType, campId, extra);
end

-- 内部函数声明
local sortPet;

-- 构造函数
function UITankDesc:ctor(tankType, campId, extra)
    -- 初始化
    self:setName("UITankDesc");
    local node = cc.CSLoader:createNode("layout/shop/TankDesc.csb");
    self:addChild(node);
    self.node = node;
    self.tankType = tankType;
    self.campId = campId;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    if not tankType and not campId and extra then
        -- 重绘
        self:redrawEx(extra);
    else
        -- 重绘
        self:redraw();
    end

    -- 适配
    self:resize();
end

-- 重绘
function UITankDesc:redrawEx(extra)
    local titleLabel = findChildByName(self.node, "CT/title");
    local descLabel = findChildByName(self.node, "CT/desc");
    local detailLabel = findChildByName(self.node, "CT/detail");
    local extraLabel = findChildByName(self.node, "CT/extra");
    local bg = findChildByName(self.node, "CT/bg");

    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    titleLabel:setString(extra.name);

    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, extra.desc);

    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    detailLabel:setString(extra.detail);

    -- 自适应
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, {titleLabel, descLabel, detailLabel});
end

-- 适配
function UITankDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UITankDesc:redraw()
    local titleLabel = findChildByName(self.node, "CT/title");
    local descLabel = findChildByName(self.node, "CT/desc");
    local detailLabel = findChildByName(self.node, "CT/detail");
    local extraLabel = findChildByName(self.node, "CT/extra");
    local bg = findChildByName(self.node, "CT/bg");

    -- 获取物品信息
    local classId = FormulaM.invoke("CALC_PUZZLE_BOX_COST_CLASS_ID", self.tankType);
    local name = ItemM.query(classId, "name");
    local desc = ItemM.query(classId, "desc");
    local detail;
--    local campName = CampM.query(self.campId, "name");
    if self.tankType == "gem" then
        detail = getLocStr("gold_tank_extra_desc");
    else
        detail = getLocStr("tank_extra_desc");
    end

    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    titleLabel:setString(name);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel:setString(desc);
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    detailLabel:setString(detail);

    local slimeSet = table.deepcopy(ME.user.dbase:query("rare_slime_setting") or {});

      -- 自适应
      AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, {titleLabel, descLabel, detailLabel});
end

-- 排序冈布奥
function sortPet(pet1, pet2)
    local slimeSet = table.deepcopy(ME.user.dbase:query("rare_slime_setting") or {});
    if (slimeSet[pet1] or 0) ~= (slimeSet[pet2] or 0) then
        return (slimeSet[pet1] or 0) < (slimeSet[pet2] or 0);
    end
    return pet1 < pet2;
end

-- 注册事件处理回调函数
function UITankDesc:registerEventCallback()
    -- 注册获得焦点的回调
    EventMgr.register("UITankDesc", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UITankDesc" then
        end
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UITankDesc 界面析构清理");
            EventMgr.removeAll("UITankDesc");
        elseif eventType == "enter" then
        end
    end);
end

-- 注册点击事件
function UITankDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UITankDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end